﻿using UnityEngine;
using System.Collections;
using System;
class GridArray
{
    //网格优化 Food 的 索引
    public GridArray()
    {
        this.Init();

    }
    public void Init()
    {
        list = new ArrayList[A_GRID_XY, A_GRID_XY];
        for (int i = 0; i < A_GRID_XY; i++)
        {
            for (int ii = 0; ii < A_GRID_XY; ii++)
            {
                list[i, ii] = new ArrayList();
            }

        }
        array = new bool[A_GRID_XY, A_GRID_XY];

    }


    public void AddObject(Food obj)
    {
        (list[GetIndex(obj.x), GetIndex(obj.y)] as ArrayList).Add(obj);
    }
    public ArrayList GetGridObject(float x, float y, float redis)
    {
        ArrayList ret = new ArrayList();

        //初始化Grid 下表false
        for (int i = 0; i < A_GRID_XY; i++)
        {
            for (int ii = 0; ii < A_GRID_XY; ii++)
            {
                array[i, ii] = false;
            }
        }

        int[] pos_factor_x = { -1, 0, 1, 1, 1, 0, -1, -1 };
        int[] pos_factor_y = { 1, 1, 1, 0, -1, -1, -1, 0 };
        int xxx, yyy;
        for (int i = 0; i < 8; i++)
        {//筛选出满足条件 设为true
            float xx = x + pos_factor_x[i] * redis;
            float yy = y + pos_factor_y[i] * redis;
            if (xx < 0.0f) continue;
            if (yy < 0.0f) continue;
            xxx = GetIndex(xx);
            yyy = GetIndex(yy);
            if (xxx > A_GRID_XY - 1 || yyy > A_GRID_XY - 1) continue;

            array[xxx, yyy] = true;
        }

        //根据Grid 下标情况筛选出Food
        for (int i = 0; i < A_GRID_XY; i++)
        {
            for (int ii = 0; ii < A_GRID_XY; ii++)
            {
                if (array[i, ii] == true)
                {
                    ArrayList objs = list[i, ii] as ArrayList;
                    foreach (Food f in objs)
                    {
                        ret.Add(f);
                    }
                }
            }
        }


        return ret;

    }

    public int GetIndex(float no)
    {
        return (int)(no / 100);
    }
    ArrayList[,] list;//Food 数组
    bool[,] array;// grid index 是否满足条件

    const int A_GRID_XY = Config.MAX_MAP_WIDTH / 100;//格子长宽

}

public class FoodsMgr : ZT_Object
{
    public static FoodsMgr ins
    {
        get
        {
            return FoodsMgr.GetInstance();
        }
    }

    private static FoodsMgr _ins = null;

    public static FoodsMgr GetInstance()
    {
        if (_ins == null)
        {
            _ins = new FoodsMgr();
        }
        return _ins;
    }


    private ArrayList objs = new ArrayList();



    public override void OnExit()
    {
        _ins.objs.Clear();
        _ins = null;
        base.OnExit();
    }
    public override bool Init()
    {
        base.Init();
        randObj = new System.Random(seed);


        for (int i = 0; i < Config.MAX_FOODS_COUNT; i++)
        {
            //  int k = i;

            //  EventDispatcher.ins.PostEvent("addAsync", new Func<int> (() =>
            {
                Food f = new Food();

                f.x = randObj.Next(0, Config.MAX_MAP_WIDTH);
                f.y = randObj.Next(0, Config.MAX_MAP_HEIGHT);

                objs.Add(f);

                ViewMgr.Create<ViewFood>(f);
                grid.AddObject(f);
                //   return k*100/Config.MAX_FOODS_COUNT ;
            }
            //    ));




        }
        return true;

    }
    public override void UpdateMS()
    {

        foreach (Food b in objs)
        {
            b.UpdateMS();//更新固定食物
        }

        foreach (Food b in _objs_child)
        {
            b.UpdateMS();//更新孢子
        }


    }


    public ArrayList GetEatFoods(Ball b)
    {
        ArrayList ret = new ArrayList();

        ArrayList foods = grid.GetGridObject(b.x, b.y, b.radis);
        //  Debug.Log("search count = " + foods.Count);

        foreach (Food food in foods)
        {
            if (!food.isEnable()) continue;//skip dis able food

            float dis = Utils.ClaculateDistance(new Vector2(b.x, b.y), new Vector2(food.x, food.y));

            //    Debug.Log(dis);
            if (dis < b.radis)
            {
                food.SetDisable();
                ret.Add(food);



            }
        }

        //scanf for FoodChild

        foreach (Food food in _objs_child)
        {
            if (!food.isEnable()) continue;//skip dis able food

            float dis = Utils.ClaculateDistance(new Vector2(b.x, b.y), new Vector2(food.x, food.y));

            //    Debug.Log(dis);
            if (dis < b.radis)
            {
                food.SetDisable();
                ret.Add(food);



            }
        }



        return ret;
    }

    public void SetRandomSeed(int s)
    {
        seed = s;
        Utils.RandomSeed = s;
        Debug.Log(" [FoodsMgr]seed = " + seed);

    }

    public void CreateChild(Ball b)
    {
        if (b.value < 20) return;

        FoodChild food = new FoodChild();

        food.x_delta = b.x_delta;
        food.y_delta = b.y_delta;

        food.x = b.x + b.radis* b.x_delta;
        food.y = b.y + b.radis * b.y_delta;
        food.Init();

        ViewMgr.Create<ViewFood>(food);//创建View
        food.Init();
        b.value -= food.value;
        this._objs_child.Add(food);

    }

    public void RemoveFoodChild(FoodChild child)
    {
        this._objs_child.Remove(child);

    }
    System.Random randObj;
    int seed;


    private GridArray grid = new GridArray();
    ArrayList _objs_child = new ArrayList();
}


